<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Page extends Controller_Client_Layout
{    
    const NEWS_CATEGORY_ID = 2;
    const PROJECT_CATEGORY_ID = 4;
    const FUNNY_CATEGORY_ID = 6;
    const FENG_SHUI_CATEGORY_ID = 7;
    const SHAREHOLDER_CATEGORY_ID = 11;
    const ABOUT_CATEGORY_ID = 9;
    const RECRUITMENT_CATEGORY_ID = 10;
    const PAPER_CATEGORY_ID = 12;
    const INTERNAL_CATEGORY_ID = 13;
    
    public function __construct(Request $request, Response $response) {
        parent::__construct($request, $response);
        
        if(Request::current()->method() === 'POST')
        {
            if(isset($_POST['order']))
            {
                Session::instance()->set('order', Security::xss_clean(Arr::get($_POST, 'order')));
            }
        }
    }
    
    public function after() {
        
        $this->set_main_right(null);
        
        parent::after();
    }
    /**
     * get articles with category id = 2
     */
    public function action_news()
    {
        if(!is_null($this->request->param('id',NULL)))
        {
            $page = ArticleTable::getInstance()->findOneById($this->request->param('id',NULL));
        
            if(!$page)
                Error::error_404 ();
            // Add +1 to views
            $page->countPageview();
            
            $this->set_contents(View::client('page/detail')->set(array(
                'page' => $page,
                'title' => 'Tin tức'
            )));
        }
        else
        {
            $list = $this->get_list(self::NEWS_CATEGORY_ID,'news');
            $this->set_contents(View::client('page/news')->set(array(
                'articles' => $list['articles'],
                'pagination' => $list['pagination'],
                'route' => 'news',
                'category' => Libs_Category::getInstance()->getCategoryById(self::NEWS_CATEGORY_ID)
            )));
        }
    }
    
    public function action_category()
    {
        $category_id = $this->request->param('id',0);
        $list = $this->get_list($category_id,'news-category',true);
        $this->set_contents(View::client('page/news')->set(array(
            'articles' => $list['articles'],
            'pagination' => $list['pagination'],
            'route' => 'news',
            'category' => Libs_Category::getInstance()->getCategoryById(self::NEWS_CATEGORY_ID) //category id tin tuc
        )));
    }
    
    /**
     * get articles with category id = 4
     */
    public function action_project()
    {
        $this->get_pages(4,'Dự án','project');
    }
    
    /**
     * get article with category id = 9
     * one page
     */
    public function action_about()
    {
        $page = Doctrine_Query::create()
                                ->from('Article a')
                                ->where('a.category_id = ?',self::ABOUT_CATEGORY_ID)
                                ->andWhere('a.status = ?',Article::STATUS_APPROVAL)
                                ->fetchOne();
        $this->set_contents(View::client('page/static')->set(array(
            'title' => 'Giới thiệu',
            'page' => $page
        )));
    }
    
    /**
     * page id = 10
     */
    public function action_recruitment()
    {
        $page = Doctrine_Query::create()
                                ->from('Article a')
                                ->where('a.category_id = ?',self::RECRUITMENT_CATEGORY_ID)
                                ->andWhere('a.status = ?',Article::STATUS_APPROVAL)
                                ->fetchOne();
        $this->set_contents(View::client('page/static')->set(array(
            'title' => 'Tuyển dụng',
            'page' => $page
        )));
    }
    
    /**
     * get articles with category id = 7
     */
    public function action_feng_shui()
    {
        $this->get_pages(self::FENG_SHUI_CATEGORY_ID,'Phong thủy','feng_shui');
    }
    
    /**
     * get articles with category id = 6
     */
    public function action_funny()
    {
        $this->get_pages(self::FUNNY_CATEGORY_ID,'Giải trí','funny');
    }
    
    /**
     * get articles with category id = 11
     */
    public function action_shareholder()
    {
        $this->get_pages(self::SHAREHOLDER_CATEGORY_ID,'Thông tin cổ đông','shareholder');
    }
    
    
    /**
     * get articles with self::PAPER_CATEGORY_ID
     */
    public function action_paper()
    {
        $this->get_pages(self::PAPER_CATEGORY_ID,'Tin báo chí','paper');
    }
    
    /**
     * get articles with self::INTERNAL_CATEGORY_ID
     */
    public function action_internal()
    {
        $this->get_pages(self::INTERNAL_CATEGORY_ID,'Tin tức nội bộ','internal');
    }
    
    /**
     * @todo display page to view
     * @param type $category_id category id
     * @param type $title title of the page
     * @param type $route route name of the page
     */
    private function get_pages($category_id,$title,$route)
    {
        //get all param from current request
        $params = $this->request->param();
        
        if(is_numeric($params['id']))
        {
            //display article with $params['id']
            $this->get_page($params['id'], $category_id, $title);
        }
        else
        {
            $list = $this->get_list($category_id, $route);
            
            $this->set_contents(View::client('page/list')->set(array(
                'articles' => $list['articles'],
                'pagination' => $list['pagination'],
                'title' => $title,
                'route' => $route
            )));
        }
    }
    
    /**
     * get one page view detail
     * @param type $id page id
     * @param type $title title of the page
     */
    private function get_page($id,$category_id,$title)
    {
        $page = ArticleTable::getInstance()->findOneById($id);
        
        if(!$page)
            Error::error_404 ();
        
        if($page->category_id != $category_id)
            Error::error_404 ();
        
        $this->set_contents(View::client('page/detail')->set(array(
            'page' => $page,
            'title' => $title
        )));
    }
    
    private function get_list($category_id,$route,$is_news_category = false)
    {
        $params = $this->request->param();
        //display list article by category
        $query = Doctrine_Query::create()
                                ->from('Article a')
                                ->where('a.category_id = ?',$category_id)
                                ->andWhere('a.status = ?',Article::STATUS_APPROVAL);
        
        $query->orderBy('a.created '.Session::instance()->get('order', 'DESC'));
        
        $pager = new Doctrine_Pager($query, $params['page'], 5);
        $pagerRange = new Doctrine_Pager_Range_Sliding(array('chunk' => 5),$pager);
        
        $urlMask = Route::url($route,array('page' => '{%page_number}'));
        
        if($is_news_category)
        {
            $category = Libs_Category::getInstance()->getCategoryById($category_id);
            if($category)
            {
                $urlMask = Route::url($route,array('id' => $category->id, 'slug' => $category->slug,'page' => '{%page_number}'));
            }
        }

        $pagination = new CustomPagerLayout($pager, $pagerRange,$urlMask);
        $pagination->setTemplate('<a href="{%url}" class="number" title="Trang {%page}">{%page}</a>');
        $pagination->setSelectedTemplate('<a href="{%url}" class="number current" title="Trang {%page}">{%page}</a>');

        return array(
            'articles' => $pager->execute(),
            'pagination' => $pagination
        );        
    }
}

